#include "play_manage.h"
#include "device.h"
#include "global.h"
#include <string.h>
#include <map>

Uint32                                  g_play_init = 0;
std::map< Uint32, play_info_t*>         g_map_play;
CMkLock                                 g_play_lock;

Uint32 play_manage_init()
{
    return NoneError;
}

void play_manage_uninit()
{
    g_play_lock.Lock();
    std::map< Uint32, play_info_t*>::iterator it = g_map_play.begin();
    for (; it != g_map_play.end(); it++)
    {
        play_info_free(it->second);
    }
    g_map_play.clear();
    g_play_lock.Unlock();
}

void play_info_free(play_info_t* playinfo)
{
    if (playinfo->dst_host)
    {
        free(playinfo->dst_host);
    }
    if (playinfo->channel_id)
    {
        free(playinfo->channel_id);
    }
    if (playinfo->invite_callid)
    {
        free(playinfo->invite_callid);
    }
    if (playinfo->src_host)
    {
        free(playinfo->src_host);
    }
    memset(playinfo, 0, sizeof(play_info_t));
    free(playinfo);
}

Uint32 get_valid_play_id()
{
    Uint32 tmpid = invalid_id;
    Uint32 try_times = 0;
    while (invalid_id == tmpid && try_times < 10)
    {
        tmpid = get_random_userid(GbPlay);
        g_play_lock.Lock();
        std::map< Uint32, play_info_t*>::iterator it = g_map_play.find(tmpid);
        if (it != g_map_play.end())
        {
            tmpid = invalid_id;
        }
        g_play_lock.Unlock();
        try_times++;
    }
    return tmpid;
}

Uint32 get_play_info(const char* callid, Uint32& playid, play_info_t** playinfo)
{
    Uint32 ret = -1;
    g_play_lock.Lock();
    std::map< Uint32, play_info_t*>::iterator it = g_map_play.begin();
    for (; it != g_map_play.end(); it++)
    {
        if (0 == strcmp(it->second->invite_callid, callid))
        {
            ret = 0;
            playid = it->first;
            *playinfo = it->second;
            break;
        }
    }
    g_play_lock.Unlock();
    return ret;
}

Uint32 get_playid_from_userid(Uint32 userid, Uint32& playid)
{
    Uint32 ret = -1;
    g_play_lock.Lock();
    auto iter = g_map_play.begin();
    for(; iter != g_map_play.end(); iter++) {
        if (iter->second->user_id == userid) {
            ret = 0;
            playid = iter->first;
            break;
        }
    }
    g_play_lock.Unlock();
    return ret;
}

Uint32 get_play_info_from_id(Uint32 id, play_info_t** playinfo)
{
    Uint32 ret = -1;
    g_play_lock.Lock();
    std::map< Uint32, play_info_t*>::iterator it = g_map_play.find(id);
    if (it != g_map_play.end())
    {
        ret = 0;
        *playinfo = it->second;
    }
    g_play_lock.Unlock();
    return ret;
}

Uint32 add_play_info_to_map(Uint32 playid, play_info_t* playinfo)
{
    g_play_lock.Lock();
    g_map_play[playid] = playinfo;
    g_play_lock.Unlock();
    return 0;
}

Uint32 del_play_info(Uint32 playid)
{
    Uint32 ret = -1;
    g_play_lock.Lock();
    std::map< Uint32, play_info_t*>::iterator it = g_map_play.find(playid);
    if (it != g_map_play.end())
    {
        ret = 0;
        play_info_free(it->second);
        g_map_play.erase(it);
    }
    g_play_lock.Unlock();
    return ret;
}

Uint32 get_play_info_count()
{
    Uint32 ret = 0;
    g_play_lock.Lock();
    ret = g_map_play.size();
    g_play_lock.Unlock();

    return ret;
}
